package com.tronsis.distribution.widget.address;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import com.tronsis.distribution.bean.RegionBean;
import com.tronsis.library.R;


public class BottomDialog extends Dialog {
    private AddressSelector selector;

    public BottomDialog(Context context, RegionBean regionBean, int level) {
        super(context, R.style.BottomDialog);
        init(context, regionBean, level);
    }

    public BottomDialog(Context context, int themeResId, RegionBean regionBean, int level) {
        super(context, themeResId);
        init(context, regionBean, level);
    }

    public BottomDialog(Context context, boolean cancelable, OnCancelListener cancelListener, RegionBean regionBean, int level) {
        super(context, cancelable, cancelListener);
        init(context, regionBean, level);
    }

    private void init(Context context, RegionBean regionBean, int level) {
        selector = new AddressSelector(context, regionBean, level);
        setContentView(selector.getView());

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = dp2px(context, 360);
        window.setAttributes(params);

        window.setGravity(Gravity.BOTTOM);
    }

    public ImageButton getCloseBtn() {
        return selector.getCloseBtn();
    }


    public void setOnAddressSelectedListener(OnAddressSelectedListener listener) {
        this.selector.setOnAddressSelectedListener(listener);
    }


    public static int dp2px(Context context, float dp) {
        return (int) Math.ceil(context.getResources().getDisplayMetrics().density * dp);
    }
}
